//http://www.webmasterworld.com/forum91/4686.htm
//myField accepts an object reference, myValue accepts the text strint to add
function insertAtCursor(myField, myValue) {
//IE support
if (document.selection) {
myField.focus();
sel = document.selection.createRange();
sel.text = myValue;
}
//Mozilla/Firefox/Netscape 7+ support
else if (myField.selectionStart || myField.selectionStart == '0') {
var startPos = myField.selectionStart;
var endPos = myField.selectionEnd;
myField.value = myField.value.substring(0, startPos)+ myValue+ myField.value.substring(endPos, myField.value.length);
} else {
myField.value += myValue;
}
myField.focus();
}

//扩展按钮
function textareaInsertInclude(id) {insertAtCursor($(id),"<!--include:"+prompt("node id")+"-->");}
function textareaInsertIncludeId(textarea_id,inc_id) {insertAtCursor($(textarea_id),"<!--include:"+inc_id+"-->");}
function textareaInsertEndInclude(id) {insertAtCursor($(id),"<!--end:include-->");}
function drawTextareaExButton() {
	var textareas=document.getElementsByTagName("textarea");
	var n=textareas.length;
	for(var i=0;i<n;i++) {
		var now_text=textareas[i];
		var now_text_id,box_id;
		now_text_id=(now_text.id.length?now_text.id:"textarea_ex_body_" + i);
		box_id="textarea_ex_button_" + i + "_select";
		var inners=Array(
			Array("param_use_include",
			"<input type='button' value='include' onclick='toggleTextareaSelectBox(\"" + box_id + "\",\"" + now_text_id + "\")'/>"),
			Array("param_use_end_include",
			"<input type='button' value='end include' onclick='textareaInsertEndInclude(\"" + now_text_id + "\")'/>"),
			Array("param_use_include",
			"<br/><div style='width:100%;display:none;' id='" + box_id + "'></div>")
		);
		var inner="";
		for(var j=0;j<inners.length;j++) {
			if(now_text.className.indexOf(inners[j][0],0)>=0)
				inner+=inners[j][1];
		}
		if(inner.length>0){
			now_text.id=now_text_id;
			var box=document.createElement("div");
			box.innerHTML=inner;
			now_text.parentNode.insertBefore(box,now_text);
		}
	}
}
function toggleTextareaSelectBox(id,textarea_id) {toggleBox(id,function(){drawTextareaSelectBox(id,textarea_id);});}
function drawTextareaSelectBox(id,textarea_id) {
	var box=$(id);
	if(box==undefined)return;
	ajaxOpen("ajax.php?action=template_list",
		function(r){
			if(r.length==0)box.innerHTML="";
			else {
				var rows=r.split("<br>");
				var table=document.createElement("table");
				var i;
				for(i=0;i<rows.length;i++) {
					var pos=rows[i].indexOf(":");
					var node_id=rows[i].substr(0,pos),node_title=rows[i].substr(pos+1);
					var tr=table.insertRow(i);
					tr.style.borderColor="black";
					tr.style.borderStyle="solid";
					tr.style.borderWidth="1px";
					tr.insertCell(0).innerHTML="<a href='shownode.php?id=" + node_id + "' target='_blank'>" + node_title + "</a>";
					tr.insertCell(1).innerHTML="<a onclick='textareaInsertIncludeId(\"" + textarea_id + "\",\"" + node_id + "\")'>Select</a>";
				}
				table.style.width="100%";
				box.innerHTML="";
				box.appendChild(table);
			}
		});
}

drawTextareaExButton();

